/**
 * PITSPortlet class.
 */
package pits.portlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.log4j.Logger;

import pits.blo.LoginBLO;
import pits.dao.AddTakenCompanyInfoDAO;
import pits.dao.AddTakenCourseDAO;
import pits.dao.AddTakenProjectDAO;
import pits.dao.DeleteTakenCourseDAO;
import pits.dao.DeleteTakenProjectDAO;
import pits.dao.EditCourseInfoDAO;
import pits.dao.EditProjectInfoDAO;
import pits.dao.EditTakenCompanyInfoDAO;
import pits.dao.EditTakenCourseInfoDAO;
import pits.dao.EditTakenProjectInfoDAO;
import pits.dao.EmployeeDAO;
import pits.dao.LoginDAO;
import pits.dao.RoleDAO;
import pits.dao.SearchInfoDAO;
import pits.dto.CompanyDTO;
import pits.dto.DepartexpDTO;
import pits.dto.EmployeeDTO;
import pits.dto.UserDTO;
import pits.form.AddTakenCompanyInfoForm;
import pits.form.AddTakenCourseForm;
import pits.form.AddTakenProjectForm;
import pits.form.DeleteTakenCourseForm;
import pits.form.DeleteTakenProjectForm;
import pits.form.EditCourseInfoForm;
import pits.form.EditEmployeeInfoForm;
import pits.form.EditProjectInfoForm;
import pits.form.EditTakenCompanyInfoForm;
import pits.form.EditTakenCourseForm;
import pits.form.EditTakenProjectForm;
import pits.form.ExportEmpInfoForm;
import pits.form.LoginForm;
import pits.form.RegisterForm;
import pits.form.SearchInfoForm;
import pits.util.Constants;
import pits.util.ExportEmpInfoUtil;

/**
 * PITSPortlet Portlet Class.
 * 
 * @author ltd
 */
public class PITSPortlet extends GenericPortlet {

	private boolean firstFlag = true;
	private String currentPage = Constants.BLANK_VALUE;
	private String nextPagePath = Constants.BLANK_VALUE;
	private String previousPath = Constants.BLANK_VALUE;
	private static Logger log = Logger.getLogger("PITSPortlet");
	private String action = Constants.BLANK_VALUE;
	private String checkedList = Constants.BLANK_VALUE;
	private int departID = -1;
	/**
	 * Forms.
	 */
	private LoginForm formLogin = null;
	private EditEmployeeInfoForm formEmp = null;
	private EditCourseInfoForm formCourseInfo = null;
	private EditProjectInfoForm formProjectInfo = null;
	private EditTakenCompanyInfoForm formTakenCompany = null;
	private EditTakenCourseForm formTakenCourse = null;
	private EditTakenProjectForm formTakenProject = null;
	private SearchInfoForm formSearchInfo = null;
	private ExportEmpInfoForm formExportEmpInfo = null;
	private AddTakenCompanyInfoForm formAddCom = null;
	private AddTakenCourseForm formAddTakenCourse = null;
	private AddTakenProjectForm formAddTakenProject = null;
	private DeleteTakenCourseForm formDeleteTakenCourse = null;
	private DeleteTakenProjectForm formDeleteTakenProject = null;
	private RegisterForm formReg = null;
	/**
	 *
	 */
	private LoginBLO bloLogin = null;
	/**
	 * Data.
	 */
	private UserDTO dtoUser = null;
	private EmployeeDTO dtoEmployee = null;
	private DepartexpDTO dtoDepexp = null;
	private CompanyDTO dtoCom = null;
	private LoginDAO daoUser = null;
	private EmployeeDAO daoEmp = null;
	private RoleDAO daoRole = null;
	private EditTakenCompanyInfoDAO daoCom = null;
	private int empIDToGetDetail;
	private List<EmployeeDTO> searchResultList;

	// private int selectedSearchEmp;
	// private List<EmployeeDTO> searchResultList;

	public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {

		PortletRequestDispatcher dispatcher;

		response.setContentType("text/html");
		log.info("doView.start");

		if (firstFlag) {
			log.debug("doView.firstFlag = true");
			nextPagePath = Constants.PATH_LOGIN;
		} else {
			log.debug("doView.firstFlag = false");
			// remove errorrMess and successMess if any
			request.getPortletSession().removeAttribute(Constants.SESSION_ATT_ERROR, PortletSession.APPLICATION_SCOPE);
			request.getPortletSession()
			        .removeAttribute(Constants.SESSION_ATT_SUCCESS, PortletSession.APPLICATION_SCOPE);

			if (action.equals(Constants.ACTION_LOGOUT)) {

				log.info("doView.logout");

				// clear all attribute in application
				previousPath = Constants.BLANK_VALUE;
				currentPage = Constants.BLANK_VALUE;

				nextPagePath = Constants.PATH_LOGIN;

				formLogin = null;
				dtoUser = null;
				dtoEmployee = null;
				bloLogin = null;
				daoEmp = null;
				daoUser = null;

				request.getPortletSession().removeAttribute(Constants.SESSION_ATT_NAME,
				        PortletSession.APPLICATION_SCOPE);
				request.getPortletSession().removeAttribute(Constants.SESSION_ATT_EMPLOYEE,
				        PortletSession.APPLICATION_SCOPE);
				request.getPortletSession().removeAttribute(Constants.SESSION_ATT_COURSE,
				        PortletSession.APPLICATION_SCOPE);
				request.getPortletSession().removeAttribute(Constants.SESSION_ATT_PROJECT,
				        PortletSession.APPLICATION_SCOPE);
				request.getPortletSession().removeAttribute(Constants.SESSION_ATT_ROLES,
				        PortletSession.APPLICATION_SCOPE);

				request.getPortletSession().removeAttribute("courseID", PortletSession.APPLICATION_SCOPE);
				request.getPortletSession().removeAttribute("projectID", PortletSession.APPLICATION_SCOPE);
				request.getPortletSession().removeAttribute("resultEmp", PortletSession.APPLICATION_SCOPE);
				request.getPortletSession().removeAttribute(Constants.SESSION_ATT_SEARCH_RESULT,
				        PortletSession.APPLICATION_SCOPE);
				request.getPortletSession().removeAttribute("userRole", PortletSession.APPLICATION_SCOPE);

				// redirect user to login page
			} else if (action.equals(Constants.ACTION_HOME)) {

				log.info("doView.home");

				// new pits.util.Logger().writeln("pageHome.role: " +
				// dtoUser.getRole());

				// redirect user to user's home page depend on user's role
				if (dtoUser.getRole().equals(Constants.ROLE_EMPLOYEE)) {
					nextPagePath = Constants.PATH_EMPLOYEE_STAT_INFO;
				} else if (dtoUser.getRole().equals(Constants.ROLE_PM)) {
					nextPagePath = Constants.PATH_SEARCH_INFO;
					formSearchInfo = new SearchInfoForm();
				} else { // dtoUser.getRole().equals(Constants.ROLE_ADMIN)
					nextPagePath = Constants.PATH_MENU;
				}

				request.getPortletSession().removeAttribute("courseID", PortletSession.APPLICATION_SCOPE);
				request.getPortletSession().removeAttribute("projectID", PortletSession.APPLICATION_SCOPE);
				request.getPortletSession().removeAttribute("resultEmp", PortletSession.APPLICATION_SCOPE);
				request.getPortletSession().removeAttribute(Constants.SESSION_ATT_SEARCH_RESULT,
				        PortletSession.APPLICATION_SCOPE);
			} else if (action.equals(Constants.ACTION_GET_EMPLOYEE_INFO)) {
				nextPagePath = Constants.PATH_EMPLOYEE_STAT_INFO;
			} else {
				if (currentPage.equals(Constants.PAGE_LOGIN)) {
					if (action.equals(Constants.ACTION_LOGIN)) {
						// validate the input data
						List<String> errorLst = formLogin.validate();

						if (errorLst.isEmpty()) {
							bloLogin = new LoginBLO();
							// check whether isValidUser
							if (bloLogin.isValidUser(formLogin.getUserName(), formLogin.getPassword())) {
								request.getPortletSession().setAttribute(Constants.SESSION_ATT_NAME,
								        formLogin.getUserName(), PortletSession.APPLICATION_SCOPE);
								daoUser = new LoginDAO();
								dtoUser = daoUser.getUser(formLogin.getUserName(), formLogin.getPassword()).get(0);

								request.getPortletSession().setAttribute(Constants.SESSION_ATT_ROLES,
								        dtoUser.getRole(), PortletSession.APPLICATION_SCOPE);

								dtoEmployee = new EmployeeDAO().getEmployeeInfo(dtoUser.getEmpID()).get(0);
								dtoEmployee.setDepartexps(new EditTakenCompanyInfoDAO().getDepartexps(dtoUser
								        .getEmpID()));
								request.getPortletSession().setAttribute(Constants.SESSION_ATT_EMPLOYEE, dtoEmployee,
								        PortletSession.APPLICATION_SCOPE);

								request.getPortletSession().setAttribute(Constants.SESSION_ATT_EMPID,
								        dtoUser.getEmpID(), PortletSession.APPLICATION_SCOPE);
								request.getPortletSession().removeAttribute(Constants.SESSION_ATT_ERROR,
								        PortletSession.APPLICATION_SCOPE);
								request.getPortletSession().setAttribute("userRole", dtoUser.getRole(),
								        PortletSession.APPLICATION_SCOPE);

								if (dtoUser.getRole().equals(Constants.ROLE_EMPLOYEE)) {
									nextPagePath = Constants.PATH_EMPLOYEE_STAT_INFO;
								} else if (dtoUser.getRole().equals(Constants.ROLE_PM)) {
									nextPagePath = Constants.PATH_SEARCH_INFO;
									formSearchInfo = new SearchInfoForm();
								} else { // user.getRole().equals(Constants.ADMIN_ROLE)
									nextPagePath = Constants.PATH_MENU;
								}
							} else {
								// show error message in case user info is
								// invalid
								nextPagePath = Constants.PATH_LOGIN;
								request.getPortletSession().setAttribute(Constants.SESSION_ATT_ERROR,
								        "User info is invalid!", PortletSession.APPLICATION_SCOPE);
								// remove user name if any
								request.getPortletSession().removeAttribute(Constants.SESSION_ATT_NAME);
							}
						} else {
							nextPagePath = Constants.PATH_LOGIN;
							request.getPortletSession().setAttribute(Constants.SESSION_ATT_ERROR, errorLst.get(0),
							        PortletSession.APPLICATION_SCOPE);
						}
					} else if (action.equals(Constants.ACTION_REGISTER)) {
						nextPagePath = Constants.PATH_REGISTER;
					}
					/*
					 * else { // redirect user to user's home page depend on
					 * user's role if
					 * (dtoUser.getRole().equals(Constants.ROLE_EMPLOYEE)) {
					 * nextPagePath = Constants.PATH_EMPLOYEE_STAT_INFO; } else
					 * if (dtoUser.getRole().equals(Constants.ROLE_PM)) {
					 * nextPagePath = Constants.PATH_EMPLOYEE_STAT_INFO; } else
					 * { //user.getRole().equals(Constants.ROLE_ADMIN)
					 * nextPagePath = Constants.PATH_MENU; } }
					 */
				} else if (currentPage.equals(Constants.PAGE_EMPLOYEE_STAT_INFO)) {

					// new pits.util.Logger().writeln("pageStat.role: " +
					// dtoUser.getRole());

					if (formLogin.getUserName().isEmpty()) {
						request.getPortletSession().removeAttribute(Constants.SESSION_ATT_NAME);
						nextPagePath = Constants.PATH_LOGIN;
					} else {

						previousPath = Constants.PATH_EMPLOYEE_STAT_INFO;

						if (action.equals(Constants.ACTION_EDIT_EMPLOYEE)) {
							nextPagePath = Constants.PATH_EDIT_EMPLOYEE_INFO;
						} else if (action.equals(Constants.ACTION_EDIT_TAKEN_COMPANY)) {
							nextPagePath = Constants.PATH_EDIT_TAKEN_COMPANY_INFO;
							for (int i = 0; i < dtoEmployee.getDepartexps().size(); i++) {
								int j = dtoEmployee.getDepartexps().get(i).getDepartment().getDepartID();
								if (departID == j) {
									log.info("departID = " + j);
									dtoDepexp = dtoEmployee.getDepartexps().get(i);
								} else {
									log.debug("departID not found!");
								}
							}
							request.getPortletSession().setAttribute(Constants.SESSION_ATT_DEPARTEXP, dtoDepexp,
							        PortletSession.APPLICATION_SCOPE);
							daoCom = new EditTakenCompanyInfoDAO();
							dtoCom = new CompanyDTO();
							dtoCom.setDepartments(daoCom.getDepartmentList(dtoDepexp.getDepartment().getCompany()
							        .getCompanyID()));
							request.getPortletSession().setAttribute(Constants.SESSION_ATT_COMPANY, dtoCom,
							        PortletSession.APPLICATION_SCOPE);
							daoRole = new RoleDAO();
							request.getPortletSession().setAttribute(Constants.SESSION_ATT_ROLES, daoRole.getRoles(),
							        PortletSession.APPLICATION_SCOPE);
						} else if (action.equals(Constants.ACTION_EDIT_TAKEN_COURSE)) {
							nextPagePath = Constants.PATH_EDIT_TAKEN_COURSE_INFO;
							request.getPortletSession().setAttribute("courseID", formTakenCourse.getCourseID(),
							        PortletSession.APPLICATION_SCOPE);
						} else if (action.equals(Constants.ACTION_EDIT_TAKEN_PROJECT)) {
							nextPagePath = Constants.PATH_EDIT_TAKEN_PROJECT_INFO;
							request.getPortletSession().setAttribute("projectID", formTakenProject.getProjectID(),
							        PortletSession.APPLICATION_SCOPE);
						} else if (action.equals(Constants.ACTION_ADD_TAKEN_COMPANY)) {
							nextPagePath = Constants.PATH_ADD_TAKEN_COMPANY_INFO;
							daoRole = new RoleDAO();
							request.getPortletSession().setAttribute(Constants.SESSION_ATT_ROLES, daoRole.getRoles(),
							        PortletSession.APPLICATION_SCOPE);
						} else if (action.equals(Constants.ACTION_ADD_TAKEN_COURSE)) {
							nextPagePath = Constants.PATH_ADD_TAKEN_COURSE;
						} else if (action.equals(Constants.ACTION_ADD_TAKEN_PROJECT)) {
							nextPagePath = Constants.PATH_ADD_TAKEN_PROJECT;
						} else if (action.equals(Constants.ACTION_DELETE_TAKEN_COMPANY)) {
							nextPagePath = Constants.PATH_EMPLOYEE_STAT_INFO;
							daoCom = new EditTakenCompanyInfoDAO();
							dtoEmployee.setDepartexps(daoCom
							        .deleteTakenCompanyInfo(checkedList, dtoEmployee.getEmpID()));
							request.getPortletSession().setAttribute(Constants.SESSION_ATT_EMPLOYEE, dtoEmployee,
							        PortletSession.APPLICATION_SCOPE);
						} else if (action.equals(Constants.ACTION_DELETE_TAKEN_COURSE)) {
							nextPagePath = Constants.PATH_EMPLOYEE_STAT_INFO;
							new DeleteTakenCourseDAO().update(formDeleteTakenCourse);
						} else if (action.equals(Constants.ACTION_DELETE_TAKEN_PROJECT)) {
							nextPagePath = Constants.PATH_EMPLOYEE_STAT_INFO;
							new DeleteTakenProjectDAO().update(formDeleteTakenProject);
						} else if (action.equals(Constants.ACTION_EDIT_COMPANY_INFO)) {
							// TODO
						} else if (action.equals(Constants.ACTION_EDIT_COURSE_INFO)) {
							nextPagePath = Constants.PATH_EDIT_COURSE_INFO;
							request.getPortletSession().setAttribute("courseID", formCourseInfo.getCourseID(),
							        PortletSession.APPLICATION_SCOPE);
						} else if (action.equals(Constants.ACTION_EDIT_PROJECT_INFO)) {
							nextPagePath = Constants.PATH_EDIT_PROJECT_INFO;
							request.getPortletSession().setAttribute("projectID", formProjectInfo.getProjectID(),
							        PortletSession.APPLICATION_SCOPE);
						} else if (action.equals(Constants.ACTION_SEARCH)) {
							nextPagePath = Constants.PATH_SEARCH_INFO;
							formSearchInfo = new SearchInfoForm();
							request.getPortletSession().setAttribute(Constants.SESSION_ATT_SEARCH_RESULT, null,
							        PortletSession.APPLICATION_SCOPE);
						}
					}
				} else if (currentPage.equals(Constants.PAGE_EDIT_EMPLOYEE_INFO)) {

					// new pits.util.Logger().writeln("pageLogin.role: " +
					// dtoUser.getRole());

					request.getPortletSession().removeAttribute(Constants.SESSION_ATT_ERROR);
					if (action.equals(Constants.ACTION_CANCEL)) {

						log.debug("action.cancel");

						// return to previous page
						nextPagePath = previousPath;
					} else if (action.equals(Constants.ACTION_CONFIRM)) {

						log.debug("action.cofirm: Update");

						List<String> errMess = formEmp.validate(formEmp);

						if (errMess.isEmpty()) {
							try {
								// new pits.util.Logger().write("" +
								// dtoUser.getEmpID());
								daoEmp = new EmployeeDAO();
								daoEmp.updateEmployeeInfo(dtoUser.getEmpID(), formEmp);
								nextPagePath = Constants.PATH_EMPLOYEE_STAT_INFO;
								currentPage = Constants.PAGE_EMPLOYEE_STAT_INFO;
								// reset session attributes
								dtoEmployee.setName(formEmp.getName());
								dtoEmployee.setBirthday(formEmp.getBirthday());
								dtoEmployee.setAddress(formEmp.getAddress());
								dtoEmployee.setEmail(formEmp.getEmail());
								dtoEmployee.setPhone(formEmp.getPhone());
								dtoEmployee.setGender(formEmp.getGender());
								request.getPortletSession().setAttribute(Constants.SESSION_ATT_EMPLOYEE, dtoEmployee,
								        PortletSession.APPLICATION_SCOPE);
								request.getPortletSession().setAttribute(Constants.SESSION_ATT_SUCCESS,
								        "Personal Information updated!", PortletSession.APPLICATION_SCOPE);

								nextPagePath = Constants.PATH_EMPLOYEE_STAT_INFO;
							} catch (Exception ex) {
								new pits.util.Logger().write("SQLException: " + ex);
								ex.printStackTrace();
								request.getPortletSession().setAttribute(Constants.SESSION_ATT_ERROR,
								        "Failed to update your information! Try again!",
								        PortletSession.APPLICATION_SCOPE);

								nextPagePath = Constants.PATH_EDIT_EMPLOYEE_INFO;
							}
						} else {

							nextPagePath = Constants.PATH_EDIT_EMPLOYEE_INFO;

							request.getPortletSession().setAttribute(Constants.SESSION_ATT_ERROR, errMess.get(0),
							        PortletSession.APPLICATION_SCOPE);
						}
					}
				} else if (currentPage.equals(Constants.PAGE_EDIT_TAKEN_COMPANY_INFO)) {
					if (action.equals(Constants.ACTION_CONFIRM)) {
						List<String> errMess = formTakenCompany.validate(dtoEmployee);
						if (errMess.isEmpty()) {
							dtoEmployee.setDepartexps(new EditTakenCompanyInfoDAO().updateTakenCompanyInfo(
							        formTakenCompany, departID, dtoEmployee.getEmpID()));
							request.getPortletSession().removeAttribute(Constants.SESSION_ATT_DEPARTEXP,
							        PortletSession.APPLICATION_SCOPE);
							request.getPortletSession().setAttribute(Constants.SESSION_ATT_EMPLOYEE, dtoEmployee,
							        PortletSession.APPLICATION_SCOPE);
							nextPagePath = Constants.PATH_EMPLOYEE_STAT_INFO;

							request.getPortletSession().setAttribute(Constants.SESSION_ATT_SUCCESS,
							        "Successfully updated information!", PortletSession.APPLICATION_SCOPE);
						} else {

							nextPagePath = Constants.PATH_EDIT_TAKEN_COMPANY_INFO;

							request.getPortletSession().setAttribute(Constants.SESSION_ATT_ERROR, errMess.get(0),
							        PortletSession.APPLICATION_SCOPE);
						}
					} else if (action.equals(Constants.ACTION_CANCEL)) {
						nextPagePath = Constants.PATH_EMPLOYEE_STAT_INFO;
						currentPage = Constants.PAGE_EMPLOYEE_STAT_INFO;
						request.getPortletSession().removeAttribute(Constants.SESSION_ATT_COMPANY,
						        PortletSession.APPLICATION_SCOPE);
						request.getPortletSession().removeAttribute(Constants.SESSION_ATT_DEPARTEXP,
						        PortletSession.APPLICATION_SCOPE);
					}
				} else if (currentPage.equals(Constants.PAGE_EDIT_TAKEN_COURSE_INFO)) {

					nextPagePath = previousPath;

					if (action.equals(Constants.ACTION_UPDATE_TAKEN_COURSE)) {

						List<String> errorList = formTakenCourse.validate();

						if (errorList.isEmpty()) {
							new EditTakenCourseInfoDAO().update(formTakenCourse);
						} else {
							nextPagePath = Constants.PATH_EDIT_TAKEN_COURSE_INFO;
							request.getPortletSession().setAttribute(Constants.SESSION_ATT_ERROR, errorList.get(0),
							        PortletSession.APPLICATION_SCOPE);
						}
					}
				} else if (currentPage.equals(Constants.PAGE_EDIT_TAKEN_PROJECT_INFO)) {

					nextPagePath = previousPath;

					if (action.equals(Constants.ACTION_UPDATE_TAKEN_PROJECT)) {

						List<String> errorList = formTakenProject.validate();

						if (errorList.isEmpty()) {
							new EditTakenProjectInfoDAO().update(formTakenProject);
						} else {
							nextPagePath = Constants.PATH_EDIT_TAKEN_PROJECT_INFO;
							request.getPortletSession().setAttribute(Constants.SESSION_ATT_ERROR, errorList.get(0),
							        PortletSession.APPLICATION_SCOPE);
						}
					}
				} else if (currentPage.equals(Constants.PAGE_EDIT_COURSE_INFO)) {

					// new pits.util.Logger().writeln("pageCourse.role: " +
					// dtoUser.getRole());

					nextPagePath = previousPath;

					if (action.equals(Constants.ACTION_UPDATE_COURSE_INFO)) {

						List<String> errorList = formCourseInfo.validate();

						if (errorList.isEmpty()) {
							new EditCourseInfoDAO().update(formCourseInfo);
						} else {
							nextPagePath = Constants.PATH_EDIT_COURSE_INFO;
							request.getPortletSession().setAttribute(Constants.SESSION_ATT_ERROR, errorList.get(0),
							        PortletSession.APPLICATION_SCOPE);
						}
					}
				} else if (currentPage.equals(Constants.PAGE_EDIT_PROJECT_INFO)) {

					// new pits.util.Logger().writeln("pageProject.role: " +
					// dtoUser.getRole());

					nextPagePath = previousPath;

					if (action.equals(Constants.ACTION_UPDATE_PROJECT_INFO)) {

						List<String> errorList = formProjectInfo.validate();

						if (errorList.isEmpty()) {
							new EditProjectInfoDAO().update(formProjectInfo);
						} else {
							nextPagePath = Constants.PATH_EDIT_PROJECT_INFO;
							request.getPortletSession().setAttribute(Constants.SESSION_ATT_ERROR, errorList.get(0),
							        PortletSession.APPLICATION_SCOPE);
						}
					}
				} else if (currentPage.equals(Constants.PAGE_ADD_TAKEN_COURSE)) {

					nextPagePath = Constants.PATH_EMPLOYEE_STAT_INFO;

					if (action.equals(Constants.ACTION_ADD_TAKEN_COURSE)) {
						new AddTakenCourseDAO().update(formAddTakenCourse);
					}
				} else if (currentPage.equals(Constants.PAGE_ADD_TAKEN_PROJECT)) {

					nextPagePath = Constants.PATH_EMPLOYEE_STAT_INFO;

					if (action.equals(Constants.ACTION_ADD_TAKEN_PROJECT)) {
						new AddTakenProjectDAO().update(formAddTakenProject);
					}
				} else if (currentPage.equals(Constants.PAGE_SEARCH_INFO)) {

					// new pits.util.Logger().writeln("pageSearch.role: " +
					// dtoUser.getRole());

					if (action.equals(Constants.ACTION_DO_SEARCH)) {

						nextPagePath = Constants.PATH_SEARCH_INFO;
						List<String> errorList = formSearchInfo.validate();

						if (errorList.isEmpty()) {
							searchResultList = new SearchInfoDAO().doSearch(formSearchInfo);
							request.getPortletSession().setAttribute(Constants.SESSION_ATT_SEARCH_RESULT,
							        searchResultList, PortletSession.APPLICATION_SCOPE);
						} else {
							request.getPortletSession().setAttribute(Constants.SESSION_ATT_ERROR, errorList.get(0),
							        PortletSession.APPLICATION_SCOPE);
						}
					} else if (action.equals(Constants.ACTION_GET_EMP_DETAIL)) {

						nextPagePath = Constants.PATH_EMPLOYEE_SEARCH_DETAIL;

						int i = 0;
						while (empIDToGetDetail != searchResultList.get(i).getEmpID()) {
							i++;
						}
						request.getPortletSession().setAttribute("resultEmp", searchResultList.get(i),
						        PortletSession.APPLICATION_SCOPE);
					} else if (action.equals(Constants.ACTION_SEARCH)) {
						nextPagePath = Constants.PATH_SEARCH_INFO;
						request.getPortletSession().removeAttribute("resultEmp", PortletSession.APPLICATION_SCOPE);
					} else if (action.equals(Constants.ACTION_EXPORT)) {
						nextPagePath = Constants.PATH_SEARCH_INFO;
						new ExportEmpInfoUtil().export(searchResultList, formExportEmpInfo);
					} else if (action.equals(Constants.ACTION_CANCEL)) {
						nextPagePath = previousPath;
					}
				} else if (currentPage.equals(Constants.PAGE_EMPLOYEE_SEARCH_DETAIL)) {

					if (action.equals(Constants.ACTION_EDIT_COURSE_INFO)) {

						previousPath = Constants.PATH_EMPLOYEE_SEARCH_DETAIL;
						nextPagePath = Constants.PATH_EDIT_COURSE_INFO;

						request.getPortletSession().setAttribute("courseID", formCourseInfo.getCourseID(),
						        PortletSession.APPLICATION_SCOPE);
					} else if (action.equals(Constants.ACTION_EDIT_PROJECT_INFO)) {

						previousPath = Constants.PATH_EMPLOYEE_SEARCH_DETAIL;
						nextPagePath = Constants.PATH_EDIT_PROJECT_INFO;

						request.getPortletSession().setAttribute("projectID", formProjectInfo.getProjectID(),
						        PortletSession.APPLICATION_SCOPE);
					} else if (action.equals(Constants.ACTION_CANCEL)) {
						nextPagePath = Constants.PATH_SEARCH_INFO;
					}
				} else if (currentPage.equals(Constants.PAGE_MENU)) {

					if (action.equals(Constants.ACTION_REQ_COURSE_LIST)) {
						nextPagePath = Constants.PATH_ADMIN_COURSE_LIST;
					} else if (action.equals(Constants.ACTION_REQ_PROJECT_LIST)) {
						nextPagePath = Constants.PATH_ADMIN_PROJECT_LIST;
					} else if (action.equals(Constants.ACTION_REQ_EMPLOYEE_LIST)) {
						nextPagePath = Constants.PATH_ADMIN_EMPLOYEE_LIST;
					}
					// else if (action.equals(Constants.ACTION_EDIT_EMPLOYEE)) {
					// nextPagePath = Constants.PATH_ADMIN_EDIT_EMPLOYEE;
					// request.getPortletSession().setAttribute("selectedEmpID",
					// "" + empIDToGetDetail, PortletSession.APPLICATION_SCOPE);
					// new pits.util.Logger().write("Data sent: " +
					// empIDToGetDetail);
					// }
					else if (action.equals(Constants.ACTION_SEARCH)) {
						nextPagePath = Constants.PATH_SEARCH_INFO;
						formSearchInfo = new SearchInfoForm();
						request.getPortletSession().setAttribute(Constants.SESSION_ATT_SEARCH_RESULT, null,
						        PortletSession.APPLICATION_SCOPE);
					}
				} else if (currentPage.equals(Constants.PAGE_ADMIN_COURSE_LIST)) {

					nextPagePath = previousPath;

					if (action.equals(Constants.ACTION_EDIT_COURSE_INFO)) {

						previousPath = Constants.PATH_ADMIN_COURSE_LIST;
						nextPagePath = Constants.PATH_EDIT_COURSE_INFO;

						request.getPortletSession().setAttribute("courseID", formCourseInfo.getCourseID(),
						        PortletSession.APPLICATION_SCOPE);
					}
				} else if (currentPage.equals(Constants.PAGE_ADMIN_PROJECT_LIST)) {

					nextPagePath = previousPath;

					if (action.equals(Constants.ACTION_EDIT_PROJECT_INFO)) {

						previousPath = Constants.PATH_ADMIN_PROJECT_LIST;
						nextPagePath = Constants.PATH_EDIT_PROJECT_INFO;

						request.getPortletSession().setAttribute("projectID", formProjectInfo.getProjectID(),
						        PortletSession.APPLICATION_SCOPE);
					}
				} else if (currentPage.equals(Constants.PAGE_REGISTER)) {
					if (action.equals(Constants.ACTION_REGISTER)) {
						List<String> errMess = new ArrayList<String>();
						errMess = formReg.validate();
						if (errMess.isEmpty()) {
							nextPagePath = Constants.PATH_LOGIN;
							request.getPortletSession().setAttribute(Constants.SESSION_ATT_SUCCESS,
							        "Congratulation, your account have been registered! Login now!",
							        PortletSession.APPLICATION_SCOPE);
						} else {
							nextPagePath = Constants.PATH_REGISTER;
							request.getPortletSession().setAttribute(Constants.SESSION_ATT_ERROR, errMess.get(0),
							        PortletSession.APPLICATION_SCOPE);
						}
					} else if (action.equals(Constants.ACTION_LOGIN)) {
						nextPagePath = Constants.PATH_LOGIN;
					}

				} else if (currentPage.equals(Constants.PAGE_ADD_TAKEN_COMPANY_INFO)) {
					if (action.equals(Constants.ACTION_CANCEL)) {
						nextPagePath = Constants.PATH_EMPLOYEE_STAT_INFO;
					} else if (action.equals(Constants.ACTION_CONFIRM)) {
						List<String> errMess = formAddCom.validate(dtoEmployee);
						if (errMess.isEmpty()) {
							nextPagePath = Constants.PATH_EMPLOYEE_STAT_INFO;
							dtoEmployee.setDepartexps(new AddTakenCompanyInfoDAO().addDepartexp(dtoUser.getEmpID(),
							        formAddCom));
							request.getPortletSession().setAttribute(Constants.SESSION_ATT_EMPLOYEE, dtoEmployee,
							        PortletSession.APPLICATION_SCOPE);
							request.getPortletSession().setAttribute(Constants.SESSION_ATT_SUCCESS,
							        "Successfully added new experience!", PortletSession.APPLICATION_SCOPE);
						} else {
							request.getPortletSession().setAttribute(Constants.SESSION_ATT_ERROR, errMess.get(0),
							        PortletSession.APPLICATION_SCOPE);
						}
					}
				}
			}
		}
		dispatcher = getPortletContext().getRequestDispatcher(nextPagePath);
		dispatcher.include(request, response);
		log.info("doView.end");
	}

	public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {

		log.info("processAction.start");
		new pits.util.Logger().write("processAction.start");

		if (request.getParameter("submitLink") != null) {
			currentPage = request.getParameter("submitLink").toString();
			new pits.util.Logger().write("processAction.submitLink = " + currentPage);
		}

		if (request.getParameter("txtAction") != null) {
			action = request.getParameter("txtAction").toString();
			new pits.util.Logger().write("processAction.action = " + action);
		}

		if (currentPage.equals(Constants.PAGE_LOGIN)) {
			firstFlag = false;
			formLogin = new LoginForm(request);
		} else if (currentPage.equals(Constants.PAGE_EMPLOYEE_STAT_INFO)) {

			if (action.equals(Constants.ACTION_EDIT_TAKEN_COMPANY)) {
				if (request.getParameter("departID") != null) {
					departID = Integer.parseInt(request.getParameter("departID"));
					// new pits.util.Logger().write("departID selected " +
					// departID);
				}
			} else if (action.equals(Constants.ACTION_EDIT_TAKEN_COURSE)) {
				formTakenCourse = new EditTakenCourseForm(request.getParameter("courseID"), dtoUser.getEmpID());
			} else if (action.equals(Constants.ACTION_EDIT_TAKEN_PROJECT)) {
				formTakenProject = new EditTakenProjectForm(request.getParameter("projectID"), dtoUser.getEmpID());
			} else if (action.equals(Constants.ACTION_DELETE_TAKEN_COMPANY)) {
				for (int i = 0; i < dtoEmployee.getDepartexps().size(); i++) {
					int id = dtoEmployee.getDepartexps().get(i).getDepartment().getDepartID();
					String str = "selectDepart" + id;
					if (request.getParameter(str) != null) {
						if (request.getParameter(str).equals("on")) {
							checkedList = (checkedList.equals("")) ? "" + id : checkedList + "," + id;
							// new
							// pits.util.Logger().write("checkedList with dto: "
							// + checkedList);
						}
					}
				}
			} else if (action.equals(Constants.ACTION_DELETE_TAKEN_COURSE)) {
				formDeleteTakenCourse = new DeleteTakenCourseForm(dtoUser.getEmpID(), request
				        .getParameter("deleteCourseList"));
			} else if (action.equals(Constants.ACTION_DELETE_TAKEN_PROJECT)) {
				formDeleteTakenProject = new DeleteTakenProjectForm(dtoUser.getEmpID(), request
				        .getParameter("deleteProjectList"));
			} else if (action.equals(Constants.ACTION_EDIT_COMPANY_INFO)) {
				// TODO
			} else if (action.equals(Constants.ACTION_EDIT_COURSE_INFO)) {
				formCourseInfo = new EditCourseInfoForm(request.getParameter("courseID"));
			} else if (action.equals(Constants.ACTION_EDIT_PROJECT_INFO)) {
				formProjectInfo = new EditProjectInfoForm(request.getParameter("projectID"));
			} else if (action.equals(Constants.ACTION_ADD_TAKEN_COURSE)) {
				formAddTakenCourse = new AddTakenCourseForm(dtoUser.getEmpID());
			} else if (action.equals(Constants.ACTION_ADD_TAKEN_PROJECT)) {
				formAddTakenProject = new AddTakenProjectForm(dtoUser.getEmpID());
			} else if (action.equals(Constants.ACTION_SEARCH)) {
				formSearchInfo = new SearchInfoForm();
			}
		} else if (currentPage.equals(Constants.PAGE_EDIT_EMPLOYEE_INFO)) {

			log.debug("currentPage = " + currentPage);

			formEmp = new EditEmployeeInfoForm(request);
		} else if (currentPage.equals(Constants.PAGE_EDIT_TAKEN_COMPANY_INFO)) {
			formTakenCompany = new EditTakenCompanyInfoForm(request);
		} else if (currentPage.equals(Constants.PAGE_EDIT_TAKEN_COURSE_INFO)) {
			if (action.equals(Constants.ACTION_UPDATE_TAKEN_COURSE)) {
				formTakenCourse.setFields(request);
			}
		} else if (currentPage.equals(Constants.PAGE_EDIT_TAKEN_PROJECT_INFO)) {
			if (action.equals(Constants.ACTION_UPDATE_TAKEN_PROJECT)) {
				formTakenProject.setFields(request);
			}
		} else if (currentPage.equals(Constants.PAGE_EDIT_COURSE_INFO)) {
			if (action.equals(Constants.ACTION_UPDATE_COURSE_INFO)) {
				formCourseInfo.setFields(request);
			}
		} else if (currentPage.equals(Constants.PAGE_EDIT_PROJECT_INFO)) {
			if (action.equals(Constants.ACTION_UPDATE_PROJECT_INFO)) {
				formProjectInfo.setFields(request);
			}
		} else if (currentPage.equals(Constants.PAGE_ADD_TAKEN_COURSE)) {
			formAddTakenCourse.setFields(request);
		} else if (currentPage.equals(Constants.PAGE_ADD_TAKEN_PROJECT)) {
			formAddTakenProject.setFields(request);
		} else if (currentPage.equals(Constants.PAGE_SEARCH_INFO)) {
			if (action.equals(Constants.ACTION_DO_SEARCH)) {
				formSearchInfo.setFields(request);
			} else if (action.equals(Constants.ACTION_GET_EMP_DETAIL)) {
				empIDToGetDetail = Integer.parseInt(request.getParameter("selectedSearchEmp"));
			} else if (action.equals(Constants.ACTION_EXPORT)) {
				formExportEmpInfo = new ExportEmpInfoForm(request.getParameter("exportEmpList"));
			} else if (action.equals(Constants.ACTION_CANCEL)) {
				action = Constants.ACTION_GET_EMP_DETAIL;
			}
		} else if (currentPage.equals(Constants.PAGE_EMPLOYEE_SEARCH_DETAIL)) {
			if (action.equals(Constants.ACTION_EDIT_COURSE_INFO)) {
				formCourseInfo = new EditCourseInfoForm(request.getParameter("courseID"));
			} else if (action.equals(Constants.ACTION_EDIT_PROJECT_INFO)) {
				formProjectInfo = new EditProjectInfoForm(request.getParameter("projectID"));
			}
		} else if (currentPage.equals(Constants.PAGE_ADMIN_COURSE_LIST)) {
			if (action.equals(Constants.ACTION_EDIT_COURSE_INFO)) {
				formCourseInfo = new EditCourseInfoForm(request.getParameter("courseID"));
			} else if (action.equals(Constants.ACTION_CANCEL)) {
				action = Constants.ACTION_REQ_COURSE_LIST;
				currentPage = Constants.PAGE_MENU;
			}
		} else if (currentPage.equals(Constants.PAGE_ADMIN_PROJECT_LIST)) {
			if (action.equals(Constants.ACTION_EDIT_PROJECT_INFO)) {
				formProjectInfo = new EditProjectInfoForm(request.getParameter("projectID"));
			} else if (action.equals(Constants.ACTION_CANCEL)) {
				action = Constants.ACTION_REQ_PROJECT_LIST;
				currentPage = Constants.PAGE_MENU;
			}
		} else if (currentPage.equals(Constants.PAGE_REGISTER)) {
			formReg = new RegisterForm();
			if (request.getParameter("txtUserName") != null) {
				formReg.setUserName(request.getParameter("txtUserName"));
			}
			if (request.getParameter("txtPassword") != null) {
				formReg.setPassword(request.getParameter("txtPassword"));
			}
		} else if (currentPage.equals(Constants.PAGE_ADD_TAKEN_COMPANY_INFO)) {
			formAddCom = new AddTakenCompanyInfoForm();
			formAddCom.setFields(request);
		}

		log.info("processAction.end");
		new pits.util.Logger().write("processAction.end");

	}
}
